import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({

  state: {
    // 存储token
    Authorization: localStorage.getItem('Authorization') ? localStorage.getItem(
      'Authorization') : '',
    username: localStorage.getItem('username') ? localStorage.getItem(
      'username') : '',
    allData: [],
    currentDate: localStorage.getItem('currentDate') ? localStorage.getItem('currentDate') : '',
  },

  getters: {
    getAuthorization: state => state.Authorization,
    getUsername: state => state.username,
    getAllData: state => state.allData,
    currentDate: state => state.currentDate,
  },

  mutations: {
    // 修改token，并将token存入localStorage
    changeAuthorization(state, item) {
      state.Authorization = item.Authorization;
      localStorage.setItem('Authorization', item.Authorization);
    },
    changeUsername: (state, item) => {
      state.username = item;
      localStorage.setItem('username', item);
    },
    changeAllData: (state, item) => {
      state.allData = item;
    },
    changeCurrentData: (state, item) => {
      state.currentDate = item;
      localStorage.setItem('currentDate', item);
    }
  },

  actions: {
    changeAuthorization(context, item) {
      context.Authorization = item.Authorization;
      localStorage.setItem('Authorization', item.Authorization);
    },
    changeUsername: (state, item) => {
      state.username = item;
      localStorage.setItem('username', item);
    },
    changeAllData: (state, item) => {
      state.allData = item;
    },
    changeCurrentData: (state, item) => {
      state.currentDate = item;
    }
  }
});

export default store;
